#include "stdafx.h"
#include "wf_modifycut.h"

#include "pf_snapper.h"


WF_ModifyCut::WF_ModifyCut(PF_Container& container,
                                       PF_GraphicView& graphicView)
        :PF_ActionInterface("Cut Entity",
                    container, graphicView) {

    cutEntity = NULL;
    cutCoord = PF_Vector(false);
}

QAction* WF_ModifyCut::createGUIAction(LX_Define::ActionType /*type*/, QObject* /*parent*/) {
	// tr("Divide")
	QAction *action = new QAction(tr("&Divide"), NULL);
	action->setIcon(QIcon(":/extui/modifycut.png"));
	//action->zetStatusTip(tr("Cut Entities"));
    return action;
}


void WF_ModifyCut::init(int status) {
    PF_ActionInterface::init(status);
}



void WF_ModifyCut::trigger() {

    PF_DEBUG->print("PF_ActionModifyCut::trigger()");

    if (cutEntity!=NULL && cutEntity->isAtomic() && cutCoord.valid &&
            cutEntity->isPointOnEntity(cutCoord)) {

        cutEntity->setHighlighted(false);
        graphicView->drawEntity(cutEntity);

        PF_Modification m(*container, graphicView);
        m.cut(cutCoord, (PF_AtomicEntity*)cutEntity);

        cutEntity = NULL;
        cutCoord = PF_Vector(false);
        setStatus(ChooseCutEntity);

        PF_DIALOGFACTORY->updateSelectionWidget(container->countSelected());
    }
}



void WF_ModifyCut::mouseMoveEvent(PF_MouseEvent* e) {
    PF_DEBUG->print("PF_ActionModifyCut::mouseMoveEvent begin");

    switch (getStatus()) {
    case ChooseCutEntity:
        break;

    case SetCutCoord:
        snapPoint(e);
        break;

    default:
        break;
    }

    PF_DEBUG->print("PF_ActionModifyTrim::mouseMoveEvent end");
}


void WF_ModifyCut::mouseReleaseEvent(PF_MouseEvent* e) {
    if (e->button()==Qt::LeftButton) {
        switch (getStatus()) {
        case ChooseCutEntity:
            cutEntity = catchEntity(e);
            if (cutEntity==NULL) {
                PF_DIALOGFACTORY->commandMessage(tr("No Entity found."));
            } else if (cutEntity->rtti()!=LX_Define::EntityLine &&
                       cutEntity->rtti()!=LX_Define::EntityArc &&
                       cutEntity->rtti()!=LX_Define::EntityCircle &&
                       cutEntity->rtti()!=LX_Define::EntityEllipse) {

                PF_DIALOGFACTORY->commandMessage(
                    tr("Entity must be a line, arc, circle or ellipse."));
            } else {
                cutEntity->setHighlighted(true);
                graphicView->drawEntity(cutEntity);
                setStatus(SetCutCoord);
            }
            break;

        case SetCutCoord:
            cutCoord = snapPoint(e);
            if (cutEntity==NULL) {
                PF_DIALOGFACTORY->commandMessage(tr("No Entity found."));
            } else if (!cutCoord.valid) {
                PF_DIALOGFACTORY->commandMessage(tr("Cutting point is invalid."));
            } else if (!cutEntity->isPointOnEntity(cutCoord)) {
                PF_DIALOGFACTORY->commandMessage(
                    tr("Cutting point is not on entity."));
            } else {
                trigger();
            }
            break;

        default:
            break;
        }
    } else if (e->button()==Qt::RightButton) {
        if (cutEntity!=NULL) {
            cutEntity->setHighlighted(false);
            graphicView->drawEntity(cutEntity);
        }
        init(getStatus()-1);
    }
}



void WF_ModifyCut::updateMouseButtonHints() {
    switch (getStatus()) {
    case ChooseCutEntity:
        PF_DIALOGFACTORY->updateMouseWidget(tr("Specify entity to cut"),
                                            tr("Cancel"));
        break;
    case SetCutCoord:
        PF_DIALOGFACTORY->updateMouseWidget(tr("Specify cutting point"),
                                            tr("Back"));
        break;
    default:
        PF_DIALOGFACTORY->updateMouseWidget("", "");
        break;
    }
}



void WF_ModifyCut::updateMouseCursor() {
    graphicView->setMouseCursor(LX_Define::CadCursor);
}



void WF_ModifyCut::updateToolBar() {

    switch (getStatus()) {
    case SetCutCoord:
        PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarSnap);
        break;
    case ChooseCutEntity:
    default:
        PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarModify);
        break;
    }
}


// EOF
